//
//  Utility.m
//  CarLifes
//
//  Created by yu on 2017/8/25.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "Utility.h"
#import "UIImage+WaterMark.h"

@implementation Utility

+ (NSString *)turnDirection:(NSString *)dir
{
    NSString *direction;
    if ([dir isEqualToString:@"北"]) {
        direction = @"南";
    } else if ([dir isEqualToString:@"东北"]){
        direction = @"西南";
    } else if ([dir isEqualToString:@"东"]){
        direction = @"西";
    } else if ([dir isEqualToString:@"东南"]){
        direction = @"西北";
    } else if ([dir isEqualToString:@"南"]){
        direction = @"北";
    } else if ([dir isEqualToString:@"西南"]){
        direction = @"东北";
    } else if ([dir isEqualToString:@"西"]){
        direction = @"东";
    } else if ([dir isEqualToString:@"西北"]){
        direction = @"东南";
    }
    return direction;
}

+ (NSDate *)stringToDateFromFormat:(NSString *)format timeStr:(NSString *)timeStr {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    return [formatter dateFromString:timeStr];
}

+ (NSString *)dateToStringFromFormat:(NSString *)format withDate:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    return [formatter stringFromDate:date];
}


+ (NSString *)directionFromAngle:(CGFloat)angle
{
//    CGFloat angle = [tempAngle floatValue];
    NSString *direction;
    
    if ((angle>=0 && angle<=5)||(angle>=355 && angle<=360)) {
        direction = @"北";
    } else if ((angle>5 && angle<85)) {
        direction = @"东北";
    } else if (angle>=85 && angle<=95) {
        direction = @"东";
    } else if (angle>95 && angle<175) {
        direction = @"东南";
    } else if (angle>=175 && angle<=185) {
        direction = @"南";
    } else if (angle>185 && angle<265) {
        direction = @"西南";
    } else if (angle>=265 && angle<=275) {
        direction = @"西";
    } else if (angle>275 && angle<355) {
        direction = @"西北";
    }
    
    return direction;
}


+ (NSAttributedString *)attributeStringWithContent:(NSString *)content keyword:(NSString *)keyword
{
    NSRange range = [[content uppercaseString] rangeOfString:[keyword uppercaseString]];
    if (range.location != NSNotFound) {
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:content];
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
        return attrString;
    } else {
        return [[NSAttributedString alloc] initWithString:content];
    }
    
}

//+ (NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords
//{
//    UIColor *color = [UIColor redColor];
//    
//    NSMutableAttributedString *attString =  [[NSMutableAttributedString alloc] initWithString:content];
//    
//    if (keyWords) {
//        
//        [keyWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//            
//            NSMutableString *tmpString = [NSMutableString stringWithString:content];
//            
//            NSRange range = [content rangeOfString:obj];
//            
//            NSInteger location = 0;
//            
//            while (range.length > 0) {
//                
//                [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:color range:NSMakeRange(location + range.location, range.length)];
//                
//                location += range.location+range.length;
//                
//                NSString *tmp = [tmpString substringWithRange:NSMakeRange(range.location + range.length, content.length - location)];
//                
//                tmpString = [NSMutableString stringWithString:tmp];
//                
//                range = [tmp rangeOfString:obj];
//            }
//        }];
//    }
//    
//    return attString;
//}

+ (UIImage *)waterImage:(UIImage *)img text:(NSString *)text font:(CGFloat)font frame:(CGRect)frame location:(NSString *)location
{
//    CGFloat hRatio = img.size.height/imageView.frame.size.height;
//    CGFloat wRatio = img.size.width/imageView.frame.size.width;
    
    float w = img.size.width;
    float h = img.size.height;

    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
//    PPLog(@"%lf==%lf",w,h);
    // 时间
    float width = frame.size.width;
    float height = frame.size.height;
    if (w>1000) {
        width = width*1.5;
        height = height*1.5;
        font = font*2.5;
    }
    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:font],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [text drawInRect:CGRectMake(frame.origin.x, frame.origin.y, width, height) withAttributes:attr];
    // 定位地址
    [location drawInRect:CGRectMake(20, 80, 1000, 50) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font],NSForegroundColorAttributeName:[UIColor whiteColor]}];

    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
    
}

@end
